Imports System.Data.SqlClient

Friend Class DBDatabase_Factory
    Inherits DBBase

    Public Function GetAllDatabasesForServer(ByVal lServerID As Long) As ArrayList
        Dim oFactory As New Database_Factory

        Dim oList As New ArrayList

        Dim sSQL As String = "select"
        sSQL = sSQL & "     a.[ID]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDDatabases a"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     a.[ServerID]=" & ValueToDBValue(lServerID)
        sSQL = sSQL & "     and a.[IsDeleted]=0"
        sSQL = sSQL & "     and a.[RevID]=("
        sSQL = sSQL & "     select"
        sSQL = sSQL & "         max(b.[RevID])"
        sSQL = sSQL & "     from"
        sSQL = sSQL & "         tDDDatabases b"
        sSQL = sSQL & "     where"
        sSQL = sSQL & "         b.[ID]=a.[ID]"
        sSQL = sSQL & "         and b.[IsWorkingCopy]=0)"

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        For Each oRow As DataRow In oData.Rows
            Dim oDatabase As New SQLAutoDocLib.Database(oRow.Item(0))
            oDatabase.Load()

            oList.Add(oDatabase)
        Next

        oData.Dispose()

        Return oList
    End Function

    Public Function GetDatabaseByName(ByVal sDatabase As String) As SQLAutoDocLib.Database
        Dim oDatabase As SQLAutoDocLib.Database = Nothing

        Dim sSQL As String = "select"
        sSQL = sSQL & "     [ID]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDDatabases"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [Name]=" & ValueToDBValue(sDatabase)
        sSQL = sSQL & "     and a.[RevID]=("
        sSQL = sSQL & "     select"
        sSQL = sSQL & "         max(b.[RevID])"
        sSQL = sSQL & "     from"
        sSQL = sSQL & "         tDDDatabases b"
        sSQL = sSQL & "     where"
        sSQL = sSQL & "         b.[ID]=a.[ID]"
        sSQL = sSQL & "         and b.[IsWorkingCopy]=0)"

        Dim oValue As Object = MyBase.ExecuteScalar(sSQL)

        If IsNumeric(oValue) Then
            oDatabase = New SQLAutoDocLib.Database(CLng(oValue))
            oDatabase.Load()
        End If

        Return oDatabase
    End Function

    Public Function GetAllRevsForDatabase(ByVal lDatabaseID As Long) As ArrayList
        Dim oFactory As New Database_Factory

        Dim oList As New ArrayList

        Dim sSQL As String = "select"
        sSQL = sSQL & "     a.[RevID]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDDatabases a"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     a.[ID]=" & ValueToDBValue(lDatabaseID)
        sSQL = sSQL & "     and a.[IsWorkingCopy]=0"
        sSQL = sSQL & " order by"
        sSQL = sSQL & "     [RevID] desc"

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        For Each oRow As DataRow In oData.Rows
            Dim oDatabase As New SQLAutoDocLib.Database(lDatabaseID, oRow.Item(0))
            oDatabase.Load()

            oList.Add(oDatabase)
        Next

        oData.Dispose()

        Return oList
    End Function
End Class
